/*
 * wtrp.h
 *
 *  Created on: Aug 10, 2012
 *      Author: adroaldo
 */

#ifndef WTRP_H_
#define WTRP_H_

#include <agent.h>
#include <packet.h>
#include <trace.h>
#include <timer-handler.h>
#include <random.h>
#include <classifier/classifier-port.h>
#include <priqueue.h>
#include <mobilenode.h>
#include <cmu-trace.h>
#include <energy-model.h>
#include <my-wtr-proto/wtr_packet.h>
#include <my-wtr-proto/node.h>

#define CURRENT_TIME                  Scheduler::instance().clock()
#define JITTER                        (Random::uniform()*0.5)

/************************************/
/*Declaracão de tipos e protótipos  */
/************************************/
class wtrp;
typedef void(wtrp::*firefunction)(void);
/*******************************************************/
/* Tipo genérico de Timer.                             */
/* Todos os créditos devem ser atribuidos ao Ke Liu em */
/* cs.binghamton.edu/~kliu/research/ns2code/index.html */
/*******************************************************/
class WTRP_Timer: public TimerHandler {
public:
	WTRP_Timer(wtrp *agent, firefunction function);
private:
	virtual void expire(Event *e);
	wtrp *agent_;
	firefunction firing_;
};

/************************/
/* Agent                */
/************************/

class wtrp: public Agent {

	/*friend classes*/
	friend class WTRP_Timer;

	/*node inclui identificador do nó*/
	node  *i_node;

	/*posicão do nó*/
	uint32_t pos_x;
	uint32_t pos_y;

	/*energia restante*/
	double node_energy;

	/*Funcões para lidar com pacotes*/
	void forward_data(Packet *p);
	void recv_packet(Packet *p);
	void send_packet();

	/*TEMP:DUAS FUNCÕES PARA TESTAR TIMER*/
	void send_wtrp_packet_timer();
	void reset_wtrp_packet_timer();


public:

	/*Para pegar logs*/
	Trace *logtarget_;

	/*Um ponteiro à fila de interface de rede que fica entre "classifier" e "link layer"*/
	PriQueue *ifqueue_;
	/*Port classifier para passar pacotes ao agente*/
	PortClassifier *dmux_;

	/*Nó*/
	MobileNode *iNode;

	/*Timers*/
	/*TEMP:SÓ TESTANDO TIMER*/
	WTRP_Timer send_packet_timer;
	WTRP_Timer reset_packet_timer;

	wtrp(nsaddr_t id);

	void recv(Packet *p, Handler *h);

	int command(int argc, const char * const *argv);

	/*Atualizacão de posicão*/
	void update_position();

	/*Atualizacão de energia*/
	void update_energy();
};
#endif /* WTRP_H_ */
